package com.wing.qualityTesting.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.qualityTesting.dao.ProcessQualityCategoryDao;
import com.wing.qualityTesting.model.entity.ProcessQualityCategory;
import com.wing.qualityTesting.model.request.ProcessQualityCategoryForm;
import com.wing.qualityTesting.model.response.NodeDataVO;
import com.wing.qualityTesting.model.response.ProcessQualityCategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 质量参数分类-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class ProcessQualityCategoryServiceImpl extends ServiceImpl<ProcessQualityCategoryDao, ProcessQualityCategory> implements ProcessQualityCategoryService {

    @Override
    public int add(ProcessQualityCategoryForm processQualityCategoryForm) {

        //校验

        ProcessQualityCategory processQualityCategory =new ProcessQualityCategory();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProcessQualityCategory.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(processQualityCategoryForm, processQualityCategory, copyOptions);

        return baseMapper.insert(processQualityCategory);
    }

    @Override
    public List<ProcessQualityCategoryVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<ProcessQualityCategoryVO> pageList4app(Page page, Map<String, Object> params) {
        List<ProcessQualityCategoryVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public ProcessQualityCategoryVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public int enable(Long id) {
        ProcessQualityCategoryVO processQualityCategoryVO=baseMapper.get4appById(id);
        if (processQualityCategoryVO!=null){
            ProcessQualityCategory processQualityCategory = new ProcessQualityCategory();
            processQualityCategory.setId(id);
            processQualityCategory.setIsDeleted(true);
            int updateCount=baseMapper.updateById(processQualityCategory);
            ProcessQualityCategory processQualityCategoryParent = new ProcessQualityCategory();
            processQualityCategoryParent.setId(processQualityCategoryVO.getParentId());
            processQualityCategoryParent.setHasEnableChild(true);
            baseMapper.updateById(processQualityCategoryParent);
            return updateCount;
        }
        return 0;
    }

    @Override
    @Transactional
    public int disable(Long id) {
        ProcessQualityCategoryVO processQualityCategoryVO=baseMapper.get4appById(id);
        if (processQualityCategoryVO!=null){
            ProcessQualityCategory processQualityCategory = new ProcessQualityCategory();
            processQualityCategory.setId(id);
            processQualityCategory.setIsDeleted(false);
            int updateCount=baseMapper.updateById(processQualityCategory);
            int count=baseMapper.selectCount(new QueryWrapper<ProcessQualityCategory>().eq("parent_id",processQualityCategory.getParentId()).eq("is_deleted",true));
            if (count==0){
                ProcessQualityCategory processQualityCategoryParent=new ProcessQualityCategory();
                processQualityCategoryParent.setId(processQualityCategoryVO.getParentId());
                processQualityCategoryParent.setHasEnableChild(false);
                baseMapper.updateById(processQualityCategoryParent);
            }
            return updateCount;
        }

        return 0;
    }

    public JsonResult getTreeData(){
        List<ProcessQualityCategoryVO> list=baseMapper.list4app(new HashMap<>());
        Map<Long,List<NodeDataVO>> map=new HashMap<>();
        for (ProcessQualityCategoryVO category:list) {
            if (map.containsKey(category.getParentId())){
                NodeDataVO nodeDataVO=new NodeDataVO();
                nodeDataVO.setValue(category.getId().toString());
                nodeDataVO.setName(category.getName());
                map.get(category.getParentId()).add(nodeDataVO);
            }else{
                List<NodeDataVO> tmpList=new ArrayList<>();
                NodeDataVO nodeDataVO=new NodeDataVO();
                nodeDataVO.setValue(category.getId().toString());
                nodeDataVO.setName(category.getName());
                tmpList.add(nodeDataVO);
                map.put(category.getParentId(),tmpList);
            }
        }
        List<NodeDataVO> rootList=map.get(-1L);
        if (rootList==null){
            return JsonResult.success();
        }
        constructTree(rootList,map);
        return JsonResult.success(rootList);
    }

    private void  constructTree(List<NodeDataVO> parentList,Map<Long,List<NodeDataVO>> dataMap){
        for (NodeDataVO node:parentList) {
            Long nodeId=Long.valueOf(node.getValue());
            List<NodeDataVO> childList=dataMap.get(nodeId);
            if (childList!=null){
                node.setChildren(childList);
                constructTree(childList,dataMap);
            }
        }
    }
}
